<template>
<div class="product-add pb50" v-loading="loading">
    <!--编辑文章-->
    <el-form size="small" :model="form" ref="form" label-width="100px">
      <div class="common-form">{{$t('plus.delivery_code')}}</div>
      <el-form-item :label="$t('plus.coupon_name')" prop="card_title">
        {{model.cardTitle}}
      </el-form-item>
      <el-form-item :label="$t('plus.code_prefix')">
        <el-input
          v-model="form.prefix"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.start_number')"
        :rules="[{  required: true,  message: ' '}]"
        prop="startNum"
      >
        <el-input
          v-model="form.startNum"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.code_length')"
        :rules="[{  required: true,  message: ' '}]"
        prop="codeLen"
      >
        <el-input
          v-model="form.codeLen"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.generate_amount')"
        :rules="[{  required: true,  message: ' '}]"
        prop="codeCount"
      >
        <el-input
          v-model="form.codeCount"
          placeholder=""
          class="max-w460"
        ></el-input>
      </el-form-item>
      <el-form-item
        :label="$t('plus.delivery_period')"
        :rules="[{  required: true,  message: ' '}]"
        prop="startTime"
      >
        <el-date-picker
          v-model="form.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          :placeholder="$t('plus.select_start_time')"
        >
        </el-date-picker>
        <span>-</span>
        <el-date-picker
          v-model="form.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          :placeholder="$t('plus.select_end_time')"
        >
        </el-date-picker>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button
          size="small"
          type="info"
          @click="cancelFunc"
          :loading="loading"
          >{{$t('plus.cancel_text')}}</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="onSubmit"
          :loading="loading"
          >{{$t('plus.submit')}}</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import CardApi from "@/api/card.js";
import Upload from "@/components/file/Upload.vue";
export default {
  components: {
    /*图片上传*/
    Upload
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*是否上传图片*/
      isupload: false,
      /*form表单数据*/
      form: {
        cardId: 0,
        prefix: "",
        startNum: "",
        codeLen: 6,
        codeCount: "",
        startTime: "",
        endTime: ""
      },
      model: {},
      /*新闻类别*/
      category: [],
      product: []
    };
  },
  created() {
    this.getDetail();
  },
  methods: {
    /*上传*/
    openUpload() {
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      let self = this;
      if (e != null) {
        this.form.imageId = e[0].fileId;
        this.form.image.filePath = e[0].filePath;
      }
      this.isupload = false;
    },
    getDetail() {
      let self = this;
      // 取到路由带过来的参数
      const params = self.$route.query.cardId;
      CardApi.toCodeCard({
        cardId: params
      }, true).then(res => {
        self.model = res.data;
        self.loading = false;
      }).catch(error => {});
    },
    /*修改文章*/
    onSubmit() {
      let self = this;
      let params = self.form;
      params.cardId = self.$route.query.cardId;
      // 取到路由带过来的参数
      CardApi.codeCard(params, true).then(data => {
        ElMessage({
          message: data.msg,
          type: "success"
        });
        self.cancelFunc();
      }).catch(error => {});
    },
    /*取消添加，返回文章列表*/
    cancelFunc() {
      this.$router.back(-1);
    }
  }
};
</script>

<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>

